﻿/**
 * ...
 * @author Sergey Mamyan
 */

package tx.squirrel.neko.core.model;


import neko.db.Object;

class Config extends Object {
	
	static var TABLE_IDS = ["key"];
	#if SQURELL_ENV_SERGEY
	public static var DATABASE_TYPE:String	= "MYSQL"; //SQLITE,MYSQL
	public static var DATABASE_NAME:String	= "squirrel";
	public static var DATABASE_HOST:String	= "vps1.tx-systems.com";
	public static var DATABASE_PORT:Int		= 3306;
	public static var DATABASE_USER:String	= "squirrel";
	public static var DATABASE_PASS:String	= "$squirrel@tx-systems.com#";
	public static var TEMPLATES_DIR:String	= "bin/templates/";
	#elseif SQURELL_ENV_DEV
	public static var DATABASE_TYPE:String	= "MYSQL"; //SQLITE,MYSQL
	public static var DATABASE_NAME:String	= "squirrel";
	public static var DATABASE_HOST:String	= "localhost";
	public static var DATABASE_PORT:Int		= 3306;
	public static var DATABASE_USER:String	= "squirrel";
	public static var DATABASE_PASS:String	= "$squirrel@tx-systems.com#";
	public static var TEMPLATES_DIR:String	= "/home/txprojects/projects/social/www/templates/";
	#else
		#error
	#end
	public static var data:Hash<String>;
	
	/*
	public static var DATABASE_TYPE:String	= "MYSQL"; //SQLITE,MYSQL
	public static var DATABASE_NAME:String	= "squirrel";
	public static var DATABASE_HOST:String	= "localhost";
	public static var DATABASE_PORT:Int		= 3306;
	public static var DATABASE_USER:String	= "squirrel";
	public static var DATABASE_PASS:String	= "$squirrel@tx-systems.com#";
	*/
	
	public static var manager = new neko.db.Manager<Config>(Config);
	
	public static function get(key:String):String {
		if (data == null) {
			load();
		}
		return data.get(key);
	}
	
	public static function set(key:String, value:String):Void {
		var conf:Config = manager.getWithKeys({ key:key });
		if (conf == null) {
			conf = new Config();
			conf.key = key;
			conf.value = value;
			conf.insert();
		}else {
			conf.value = value;
			conf.update();
		}
	}
	
	public static function load():Void {
		var fields:Iterator<Config> =  manager.all().iterator();
		data = new Hash<String>();
		while (fields.hasNext()) {
			var field:Config = fields.next();
			data.set(field.key, field.value);
		}
	}
	
	public static function save():Void {
		var fields:Iterator<String> =  data.keys();
		while (fields.hasNext()) {
			var fieldsName:String =  fields.next();
			var field:String = data.get(fieldsName);
			set(fieldsName, field);
		}
	}
	
	public var key:String;
	public var value:String;
	public var description:String;
	
}